<?php
/**
 * Created by PhpStorm.
 * User: 12297
 * Date: 2018/6/22
 * Time: 0:01
 */
namespace app\lib\feieyun;

use app\lib\exception\ParameterException;
use think\Exception;

class Prints{
    /*
     *  添加打印机
     *  正确例子：{"msg":"ok","ret":0,"data":{"ok":["sn#key#remark#carnum","316500011#abcdefgh#快餐前台"],"no":["316500012#abcdefgh#快餐前台#13688889999  （错误：识别码不正确）"]},"serverExecutedTime":3}
	 *  错误：{"msg":"参数错误 : 该帐号未注册.","ret":-2,"data":null,"serverExecutedTime":37}
     * 	提示：打印机编号(必填) # 打印机识别码(必填) # 备注名称(选填) # 流量卡号码(选填)，多台打印机请换行（\n）添加新打印机信息，每次最多100行(台)。
     *  $snlist = "sn1#key1#remark1#carnum1\nsn2#key2#remark2#carnum2";
     */

    public static function addPrinter($snlist,$type = 1){
        try{
            if(!empty($snlist) && ($type==1 || $type==2)){
                $time = time();
                $data = [
                    'user' => config('printer.printFeieyunUser'),
                    'stime' => $time,
                    'sig' => sha1(config('printer.printFeieyunUser').config('printer.printFeieyunUkey').$time),
                    'apiname' => 'Open_printerAddlist',
                    'printerContent' => $snlist

                ];
                $client = new FeieyunClient(config('printer.printFeieyunIp'),config('printer.printFeieyunPort'));
                if(!$client->post(config('printer.printFeieyunPath'),$data)){
                    return json_encode(['msg' => 'error']);
                }else{
                    return $client->getContent();
                }
            }
            throw new ParameterException([
                'msg' => '参数不正确'
            ]);
        }catch (Exception $ex){
            return false;
        }
    }

    public static function deletePrinter($code){
        try{
            $time = time();
            $data = [
                'user' => config('printer.printFeieyunUser'),
                'stime' => $time,
                'sig' => sha1(config('printer.printFeieyunUser').config('printer.printFeieyunUkey').$time),
                'apiname' => 'Open_printerDelList',
                'snlist' => $code
            ];
            $client = new FeieyunClient(config('printer.printFeieyunIp'),config('printer.printFeieyunPort'));
            if(!$client->post(config('printer.printFeieyunPath'),$data)){
                return json_encode(['msg' => 'error']);
            }else{
                return $client->getContent();
            }
        }catch (Exception $ex){
            return false;
        }
    }


    /*
     *  打印
     *  正确例子：{"msg":"ok","ret":0,"data":"316500004_20160823165104_1853029628","serverExecutedTime":6}
     *  错误：{"msg":"错误信息.","ret":非零错误码,"data":null,"serverExecutedTime":5}
     */
    public static function prints($sn = '217500531', $times = 1, $content = null){
        /*$orderInfo = '';
        $orderInfo = '<CB>测试打印</CB><BR>';
        $orderInfo .= '名称　　　　　 单价  数量 金额<BR>';
        $orderInfo .= '--------------------------------<BR>';
        $orderInfo .= '饭　　　　　 　10.0   10  10.0<BR>';
        $orderInfo .= '炒饭　　　　　 10.0   10  10.0<BR>';
        $orderInfo .= '蛋炒饭　　　　 10.0   100 100.0<BR>';
        $orderInfo .= '鸡蛋炒饭　　　 100.0  100 100.0<BR>';
        $orderInfo .= '西红柿炒饭　　 1000.0 1   100.0<BR>';
        $orderInfo .= '西红柿蛋炒饭　 100.0  100 100.0<BR>';
        $orderInfo .= '西红柿鸡蛋炒饭 15.0   1   15.0<BR>';
        $orderInfo .= '备注：加辣<BR>';
        $orderInfo .= '--------------------------------<BR>';
        $orderInfo .= '合计：xx.0元<BR>';
        $orderInfo .= '送货地点：广州市南沙区xx路xx号<BR>';
        $orderInfo .= '联系电话：13888888888888<BR>';
        $orderInfo .= '订餐时间：2014-08-08 08:08:08<BR>';
        $orderInfo .= '<QR>http://www.dzist.com</QR>';
        $orderInfo .= '<PLUGIN>';
        $orderInfo .= '<BR>';
        $orderInfo .= '<BR>';
        $orderInfo .= '<BR>';
        $orderInfo .= '<CUT>';*/

        $time = time();
        $data = [
            'user' => config('printer.printFeieyunUser'),
            'stime' => $time,
            'sig' => sha1(config('printer.printFeieyunUser').config('printer.printFeieyunUkey').$time),
            'sn' => $sn,
            'content' => $content,
            'apiname' => 'Open_printMsg',
            'times' => $times
        ];
        $client = new FeieyunClient(config('printer.printFeieyunIp'),config('printer.printFeieyunPort'));
        $result = $client->post(config('printer.printFeieyunPath'),$data);
        if(!$result){
            return false;
        }else{
            return true;
        }

    }
}